package com.googlecode.horncomb.web.extremetable.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import static org.apache.commons.lang.Validate.*;
import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.core.Preferences;
import org.extremecomponents.table.core.PreferencesConstants;
import org.extremecomponents.table.core.Registry;
import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.core.TableModelImpl;
import org.extremecomponents.table.core.TableModelUtils;
import org.extremecomponents.table.core.TableProperties;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.LimitRegistry;
import org.extremecomponents.table.limit.Sort;
import org.extremecomponents.table.limit.TableLimitFactory;

import com.googlecode.horncomb.orm.support.order.Order;
import com.googlecode.horncomb.orm.support.order.OrderImpl;
import com.googlecode.horncomb.orm.support.order.SortTokenImpl;
import com.googlecode.horncomb.orm.support.paged.PagedSetting;
import com.googlecode.horncomb.orm.support.paged.PagedSettingImpl;

import javax.servlet.http.HttpServletRequest;

/**
 * 辅助ExtremeTable获取分页信息的Util
 */
public class ExtremeTableUtil {

	public static Map<String, PagedSetting> resolvePagedSettings(
			HttpServletRequest request) {
		notNull(request);
		Map<String, PagedSetting> result = new HashMap<String, PagedSetting>();
		Context context = new HttpServletRequestContext(request);
		String[] tabIds = request
				.getParameterValues(TableConstants.EXTREME_COMPONENTS_INSTANCE);
		if (tabIds != null) {
			for (String tabId : tabIds) {
				result.put(tabId, getPagedSetting(context, tabId));
			}
		} else {
			result.put(TableConstants.EXTREME_COMPONENTS, getPagedSetting(
					request, TableConstants.EXTREME_COMPONENTS));
		}
		return result;
	}

	public static PagedSetting getDefaultPagedSetting(HttpServletRequest request) {
		Context context = new HttpServletRequestContext(request);
		String[] tabIds = request
				.getParameterValues(TableConstants.EXTREME_COMPONENTS_INSTANCE);
		if (tabIds != null && tabIds.length > 1) {
			return getPagedSetting(context, tabIds[0]);
		} else {
			return getPagedSetting(context, null);
		}
	}

	public static PagedSetting getPagedSetting(HttpServletRequest request,
			String tableId) {
		Context context = new HttpServletRequestContext(request);
		return getPagedSetting(context, tableId);
	}

	private static PagedSetting getPagedSetting(Context context, String tableId) {
		notNull(context);
		if (StringUtils.isEmpty(tableId)) {
			tableId = TableConstants.EXTREME_COMPONENTS;
		}

		// build pageSize
		Preferences preferences = new TableProperties();
		preferences.init(null, TableModelUtils.getPreferencesLocation(context));
		String state = preferences
				.getPreference(PreferencesConstants.TABLE_STATE
						+ TableConstants.STATE_DEFAULT);
		String stateAttr = preferences
				.getPreference(PreferencesConstants.TABLE_STATE_ATTR);

		String prefixWithTableId = tableId + "_";
		Registry registry = new LimitRegistry(context, tableId,
				prefixWithTableId, state, stateAttr);

		TableModel tabMod = new TableModelImpl(context);
		String defaultRowsDisplayed = tabMod.getPreferences().getPreference(
				PreferencesConstants.TABLE_ROWS_DISPLAYED);
		String currentRowsDisplayed = registry.getParameter(prefixWithTableId
				+ TableConstants.CURRENT_ROWS_DISPLAYED);
		int pageSize = Integer.parseInt(StringUtils.defaultIfEmpty(
				currentRowsDisplayed, defaultRowsDisplayed));

		// build order
		LimitFactory limitFactory = new TableLimitFactory(context, tableId);
		Sort sort = limitFactory.getSort();
		Order order = null;
		if (sort != null && StringUtils.isNotEmpty(sort.getProperty())) {
			order = getOrder(sort);
		}

		// page
		int page = limitFactory.getPage();

		// pageSize = limitFactory.getCurrentRowsDisplayed(totalRows,
		// defaultRowsDisplayed);

		return new PagedSettingImpl(page, pageSize, 0, null, order);
	}

	/**
	 * 将Limit中的排序信息转化为Order,升序/降序}
	 */
	static public Order getOrder(Sort sort) {
		Order order = null;
		String fieldName = StringUtils.defaultIfEmpty(sort.getAlias(), sort
				.getProperty());
		order = new OrderImpl(new SortTokenImpl(fieldName, !StringUtils
				.equalsIgnoreCase("desc", sort.getSortOrder())));
		return order;
	}
}
